package gui;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import model.Position;
import model.Node;
import model.Observation;
import model.Zone;

import comm.UdpCommsPackage;

public class NodeUIFrame implements ActionListener, Observer{
	
	private UdpCommsPackage udpComms;
	private JTextArea textArea; 
    Map<Position,List<Observation>> mp=new HashMap<Position, List<Observation>>();
	Position location = new Position(2, 1, "Zone A");
	Node localNode;
	public NodeUIFrame(){
		JFrame frame = new JFrame("Context Sharing:  Node Module");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		int width = 1100;
		int height = 900;
		
		frame.setSize(width, height);
		
		//create textArea and add to frame
		
		textArea = new JTextArea();
		textArea.setEditable(false);
		
		frame.add(new JScrollPane(textArea), BorderLayout.CENTER);
		frame.setVisible(true);
		try{
			localNode = new Node(location, new Zone("A", 30, 10), "1", ".", 6, 1);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			udpComms = new UdpCommsPackage("Zone A");
			udpComms.addObserver(this);
			
			//Create JPanel of menu options
			JPanel menuPanel = new JPanel();
			menuPanel.setLayout(new GridLayout(1, 3));
			
			//create buttons
			JButton broadcast = new JButton("Broadcast Node Observation");
			broadcast.addActionListener(this);
			menuPanel.add(broadcast);
			
			JButton clear = new JButton("Clear Screen");
			clear.addActionListener(this);
			menuPanel.add(clear);
			
			//add menuPanel to frame
			frame.add(menuPanel, BorderLayout.SOUTH);
			
			frame.validate();
			
		} catch (IOException e) {
			textArea.setText("UDP Comms IO Exception\n");
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		

	}

	public static void main(String[] args) {
		new NodeUIFrame();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if(command.equals("Broadcast Node Observation")){
		    try {
				localNode.run();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		if (command.equals("Clear Screen")){
			//clears the screen
			textArea.setText("");
		}
	}
	@Override
	public void update(Observable arg0, Object eventObject) {
			SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss");
			Date date;
			String nodeLocation="";
		Observation observation = (Observation) eventObject;

		//check to ensure that the zone from the event matches the zone that the Node is in 
		if (observation.getLocation().getZone().equals(localNode.getLocation().getZone())){
			date = new Date(observation.getTimeStamp());
			try {
				nodeLocation =InetAddress.getLocalHost().getHostAddress()+Thread.currentThread().getName()+ ", Node location: ("+localNode.getLocation().getX()+", "+localNode.getLocation().getY()+", "+localNode.getLocation().getZone()+") ";
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
			textArea.setText(textArea.getText()+nodeLocation+" Observation: "+observation.getObservations().toString()+ " at "+sdf.format(date)+"\n");
		}

	}
}
